package com.ktjy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.dto.PostDTO;
import com.ktjy.dto.RoleDTO;
import com.ktjy.mapper.SysPostMapper;
import com.ktjy.mapper.SysRoleDeptMapper;
import com.ktjy.mapper.SysRoleMenuMapper;
import com.ktjy.pojo.*;
import com.ktjy.mapper.SysRoleMapper;
import com.ktjy.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色信息表 服务实现类
 * </p>
 *
 * @author jjx
 * @since 2025-07-31
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
    @Autowired
    SysRoleMapper sysRoleMapper;
    @Autowired
    SysRoleMenuMapper sysRoleMenuMapper;
    @Autowired
    SysRoleDeptMapper sysRoleDeptMapper;

    @Override
    public IPage<SysRole> findPage(RoleDTO roleDTO, Integer pageNo, Integer pageSize) {
        //构造分页条件
        Page page=new Page(pageNo,pageSize);
        //构造查询条件
        QueryWrapper qw=new QueryWrapper();
        if(roleDTO.getRoleName()!=null && !roleDTO.getRoleName().isEmpty()){
            qw.like("role_name",roleDTO.getRoleName());
        }
        if(roleDTO.getStatus()!=null && !roleDTO.getStatus().isEmpty()){
            qw.eq("status",roleDTO.getStatus());
        }
        return sysRoleMapper.selectPage(page,qw);
    }

    @Override
    public void saveOrUpdateRole(SysRole sysRole) {
        Long roleId=sysRole.getRoleId();
        //保存用户信息
        this.saveOrUpdate(sysRole);

        if(roleId!=null){//说明是修改
            //先删除角色对应的权限
            QueryWrapper roleQw=new QueryWrapper();
            roleQw.eq("role_id",sysRole.getRoleId());
            sysRoleMenuMapper.delete(roleQw);
        }
        //批量新增角色对应权限信息
        batchInsertRoleMenu(sysRole.getMenuIds(), sysRole.getRoleId());
    }

    @Override
    public SysRole findRole(Long roleId) {
        //1.查询角色信息
        SysRole sysRole = this.getById(roleId);

        //2.查询角色关联的权限信息
        QueryWrapper<SysRoleMenu> menuQw=new QueryWrapper<SysRoleMenu>();
        menuQw.eq("role_id",roleId);
        List<SysRoleMenu> sysUserRoles = sysRoleMenuMapper.selectList(menuQw);

        List<Long> menuList = new ArrayList<Long>();
        for (SysRoleMenu sysRoleMenu : sysUserRoles) {
            menuList.add(sysRoleMenu.getMenuId());
        }
        sysRole.setMenuIds(menuList.toArray(new Long[0]));

        //3.查询角色关联的部门数据信息
        QueryWrapper<SysRoleDept> deptQw=new QueryWrapper<SysRoleDept>();
        deptQw.eq("role_id",roleId);
        List<SysRoleDept> sysUserDepts = sysRoleDeptMapper.selectList(deptQw);

        List<Long> deptList = new ArrayList<Long>();
        for (SysRoleDept sysUserDept : sysUserDepts) {
            deptList.add(sysUserDept.getDeptId());
        }
        sysRole.setDeptIds(deptList.toArray(new Long[0]));

        return sysRole;
    }

    @Override
    public void deleteRole(Long roleId) {
        //1.删除角色对应的权限信息
        QueryWrapper<SysRoleMenu> menuQw=new QueryWrapper<SysRoleMenu>();
        menuQw.eq("role_id",roleId);
        sysRoleMenuMapper.delete(menuQw);
        //2.删除角色信息
        this.removeById(roleId);
    }

    /**
     * 批量新增角色对应的权限
     * @param menus
     * @param roleId
     */
    private void batchInsertRoleMenu(Long[] menus,Long roleId){
        if (menus!=null && menus.length>0) {
            List<SysRoleMenu> list = new ArrayList<SysRoleMenu>();
            for (Long menuId : menus) {
                SysRoleMenu rm = new SysRoleMenu();
                rm.setRoleId(roleId);
                rm.setMenuId(menuId);
                list.add(rm);
            }
            if (list.size() > 0) {
                sysRoleMenuMapper.insert(list);
            }
        }
    }

    @Override
    public void assignDataScope(SysRole sysRole) {
        //1.修改角色的数据权限
        this.updateById(sysRole);
        //2.先删除角色对应的部门数据权限
        QueryWrapper deptQw=new QueryWrapper();
        deptQw.eq("role_id",sysRole.getRoleId());
        sysRoleDeptMapper.delete(deptQw);
        //3.批量添加角色对应的部门数据权限
        batchInsertRoleDept(sysRole.getDeptIds(),sysRole.getRoleId());
    }

    /**
     * 批量新增角色对应的数据权限
     * @param depts
     * @param roleId
     */
    private void batchInsertRoleDept(Long[] depts,Long roleId){
        if (depts!=null && depts.length>0) {
            List<SysRoleDept> list = new ArrayList<SysRoleDept>();
            for (Long deptId : depts) {
                SysRoleDept rd = new SysRoleDept();
                rd.setRoleId(roleId);
                rd.setDeptId(deptId);
                list.add(rd);
            }
            if (list.size() > 0) {
                sysRoleDeptMapper.insert(list);
            }
        }
    }
}
